package si.banst.sledi.ejb;

import java.util.logging.Logger;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import si.banst.sledi.ZapisSledi;

/**
 * Message-Driven Bean implementation class for: SlediMDB
 * 
 */
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }, mappedName = "jms/vrstaSledi")
public class SlediMDB implements MessageListener {
	private static final Logger logger = Logger.getLogger(SlediMDB.class
			.getName());

	@EJB
	private ShrambaSlediBean shrambaSlediBean;

	/**
	 * Default constructor.
	 */
	public SlediMDB() {
		super();
	}

	/**
	 * @see MessageListener#onMessage(Message)
	 */
	public void onMessage(Message sporocilo) {

		if (sporocilo instanceof ObjectMessage) {
			ObjectMessage sporociloObjekt = (ObjectMessage) sporocilo;
			try {
				Object objekt = sporociloObjekt.getObject();
				if (objekt instanceof ZapisSledi) {
					ZapisSledi zapisSledi = (ZapisSledi) objekt;
					shrambaSlediBean.zapisiSled(zapisSledi);

				} else {
					logger.severe("Sporočilo v vrsti ni instanca objekta tipa ZapisSledi");
				}

			} catch (Exception e) {
				logger.severe("Napaka pri prevzemu sporočila. "
						+ e.getMessage());
				throw new EJBException("Napaka pri prevzemu sporočila.", e);
			}

		}

	}

}
